Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Runtime.Serialization
Imports System.ComponentModel
Imports ATS.Business.Entities.EntityStateClass


<DataContract(IsReference:=True)>
<KnownType(GetType(MainAssetsTable))>
Public Class Location
    Implements INotifyPropertyChanged, IEditableObject, IObjectWithChangeTracker
    Private _isDirty As Boolean
    Private locationData As Location

#Region "Load Events"
    Public Sub New()
    End Sub

    Public Sub New(LocationId As Integer, _
                   LocationName As String, _
                   LocationCode As String, _
                   LocationDesc As String, _
                   IsActive As Boolean)
        _IsActive = IsActive
        _LocationId = LocationId
        _LocationName = LocationName
        _LocationCode = LocationCode
        _LocationDesc = LocationDesc
    End Sub
#End Region

#Region "Primitive Properties"
    <DataMember()>
    Public Overridable Property LocationId As Integer

    Private _LocationName As String
    <DataMember()>
    Public Property LocationName() As String
        Get
            Return _LocationName
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _LocationName Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LocationName")
                End If
            Else
                Throw New Exception("Please provide value for Location name.")
            End If
            _LocationName = value
            OnPropertyChanged("LocationName")

        End Set
    End Property

    Private _LocationCode As String
    <DataMember()>
    Public Property LocationCode() As String
        Get
            Return _LocationCode
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _LocationCode Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LocationCode")
                End If
            Else
                Throw New Exception("Please provide value for Location Code.")
            End If
            _LocationCode = value
            OnPropertyChanged("LocationCode")
        End Set
    End Property

    Private _LocationDesc As String
    <DataMember()>
    Public Property LocationDesc() As String
        Get
            Return _LocationDesc
        End Get

        Set(ByVal value As String)
            If value <> _LocationDesc Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "LocationDesc")
            End If
            _LocationDesc = value
            OnPropertyChanged("LocationDesc")
        End Set
    End Property

    Private _IsActive As Boolean
    <DataMember()>
    Public Property IsActive() As Boolean
        Get
            Return _IsActive
        End Get

        Set(ByVal value As Boolean)

            If value <> _IsActive Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "IsActive")
            End If

            _IsActive = value
            OnPropertyChanged("IsActive")
        End Set
    End Property

    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get
    End Property

#End Region
#Region "Navigation Properties"

    <DataMember()>
    Public Property MainAssetsTables() As TrackableCollection(Of MainAssetsTable)
        Get
            If _mainAssetsTables Is Nothing Then
                _mainAssetsTables = New TrackableCollection(Of MainAssetsTable)
                AddHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
            End If
            Return _mainAssetsTables
        End Get
        Set(ByVal value As TrackableCollection(Of MainAssetsTable))
            If _mainAssetsTables IsNot value Then
                If ChangeTracker.ChangeTrackingEnabled Then
                    Throw New InvalidOperationException("Cannot set the FixupChangeTrackingCollection when ChangeTracking is enabled")
                End If
                If _mainAssetsTables IsNot Nothing Then
                    RemoveHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
                End If
                _mainAssetsTables = value
                If _mainAssetsTables IsNot Nothing Then
                    AddHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
                End If
                OnNavigationPropertyChanged("MainAssetsTables")
            End If
        End Set
    End Property

    Private _mainAssetsTables As TrackableCollection(Of MainAssetsTable)

#End Region
#Region "ChangeTracking"

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        If ChangeTracker.State <> ObjectState.Added AndAlso ChangeTracker.State <> ObjectState.Deleted Then
            ChangeTracker.State = ObjectState.Modified
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Protected Overridable Sub OnNavigationPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _changeTracker As ObjectChangeTracker

    <DataMember()>
    Public Property ChangeTracker() As ObjectChangeTracker Implements IObjectWithChangeTracker.ChangeTracker
        Get
            If _changeTracker Is Nothing Then
                _changeTracker = New ObjectChangeTracker()
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            Return _changeTracker
        End Get
        Set(ByVal value As ObjectChangeTracker)
            If _changeTracker IsNot Nothing Then
                RemoveHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            _changeTracker = value
            If _changeTracker IsNot Nothing Then
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
        End Set
    End Property

    Private Sub HandleObjectStateChanging(ByVal sender As Object, ByVal e As ObjectStateChangingEventArgs)
        If e.NewState = ObjectState.Deleted Then
            Me.ClearNavigationProperties()
        End If
    End Sub

    Private _isDeserializing As Boolean
    Protected Property IsDeserializing() As Boolean
        Get
            Return _isDeserializing
        End Get
        Private Set(ByVal value As Boolean)
            _isDeserializing = value
        End Set
    End Property

    <OnDeserializing()>
    Public Sub OnDeserializingMethod(ByVal context As StreamingContext)
        IsDeserializing = True
    End Sub

    <OnDeserialized()>
    Public Sub OnDeserializedMethod(ByVal context As StreamingContext)
        IsDeserializing = False
        ChangeTracker.ChangeTrackingEnabled = True
    End Sub

    Protected Overridable Sub ClearNavigationProperties()
        MainAssetsTables.Clear()
    End Sub

#End Region
#Region "Association Fixup"

    Private Sub FixupMainAssetsTables(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        If IsDeserializing Then
            Return
        End If

        If e.NewItems IsNot Nothing Then
            For Each item As MainAssetsTable In e.NewItems
                item.Location = Me
                If ChangeTracker.ChangeTrackingEnabled Then
                    If Not item.ChangeTracker.ChangeTrackingEnabled Then
                        item.StartTracking()
                    End If
                    ChangeTracker.RecordAdditionToCollectionProperties("MainAssetsTables", item)
                End If
            Next
        End If

        If e.OldItems IsNot Nothing Then
            For Each item As MainAssetsTable In e.OldItems
                If item.Location Is Me Then
                    item.Location = Nothing
                End If
                If ChangeTracker.ChangeTrackingEnabled Then
                    ChangeTracker.RecordRemovalFromCollectionProperties("MainAssetsTables", item)
                End If
            Next
        End If
    End Sub

#End Region

#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
        locationData = New Location
        locationData._LocationName = LocationName
        locationData._LocationCode = LocationCode
        locationData._LocationDesc = LocationDesc
        locationData._IsActive = IsActive
    End Sub

    Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
        If Not locationData Is Nothing Then
            LocationName = locationData._LocationName
            LocationCode = locationData._LocationCode
            LocationDesc = locationData._LocationDesc
            IsActive = locationData._IsActive
        End If

    End Sub

    Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
        locationData = Nothing
    End Sub

    'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    'Public Sub OnPropertyChanged(ByVal propertyName As String)
    '    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    'End Sub

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Private Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState
        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState
        End If
    End Sub
#End Region
End Class
